<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\userrecharge\api\controller;

use app\api\BaseController;
use plugins\pay\addons\Alipay;
use plugins\pay\addons\Wechat;
use plugins\userrecharge\api\model\UserRecharge;
/**
 * 会员充值
 */
class User extends BaseController
{
    /**
     * 用户权鉴
     */
    public $middleware = [\app\api\middleware\AuthCheck::class];

    /**
     * 充值记录
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            $where[] = ['user_id', '=', $this->request->userId];
            $search  = ['date', 'status'];
            $append  = ['c_status', 'method'];
            $count = UserRecharge::withSearch($search, $input)->where($where)->count('id');
            $data  = UserRecharge::withSearch($search, $input)->where($where)->append($append)->order('create_time', 'desc')->page($input['page'], 10)->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }

    /**
     * 创建充值订单
     */
    public function create()
    {
        if ($this->request->isPost()) {
            if (input('post.price') > 0) {
                $order  = UserRecharge::create([
                    'id'           => rand_id(16),
                    'user_id'      => $this->request->userId,
                    'title'        => '系统充值',
                    'pay_rmb'      => input('post.price'),
                    'pay_method'   => 0,
                    'status'       => 1,
                    'pay_time'     => date('Y-m-d H:i:s'),
                    'create_time'  => date('Y-m-d H:i:s'),
                ]);
                return json(['status' => 'success','message' => '订单创建成功', 'data' => $order]);
            } else {
                return json(['status' => 'error','message' => '充值金额必须大于0', 'data' => $order]);
            }
        }
    }
     
    /**
     * 查询充值详情
     */
    public function order()
    {
        if ($this->request->isPost()) {
            $order = UserRecharge::where('id', input('id'))->find();
            $order->invalid = strtotime($order->create_time) + (60*30);
            if ($order->invalid < time()) {
                $order->status = 4;
                $order->save();
            }
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $order]);
        }
    }


    /**
     * 查询充值详情
     */
    public function orderStatus()
    {
        if ($this->request->isPost()) {
            $status = UserRecharge::where('id', input('id'))->value('status');
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $status]);
        }
    }

    /**
     * 更换支付方式
     */
    public function payMethod()
    {
        if ($this->request->isPost()) {
            $input = input('post.');
            $order = UserRecharge::where('id', $input['id'])->where('status', 1)->find();
            if (empty($order)) {
                return json(['status' => 'error', 'message' => '订单状态不正确']);
            }
            $order->pay_method = $input['method'];
            $order->save();
            if ($input['method'] == 0) {
                $result = Alipay::qrCodePay($order['id'],$order['title'],$order['pay_rmb'], index_url('api/userrecharge/notify/alipayPayNotify'));
            }
            if ($input['method'] == 1) {
                $result = Wechat::qrCodePay($order['id'],$order['title'],$order['pay_rmb'], index_url('api/userrecharge/notify/wechatPayNotify'));
            }
            return json($result);
        }
    }
}